package middleware

import (
	"github.com/astaxie/beego/context"
	"net/http"
)

func CrossDomain(ctx *context.Context) {
	//跨域设置
	ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
	ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
	ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "Access-Control-Allow-Origin,ContentType,Authorization,accept,accept-encoding, authorization, content-type")
	ctx.ResponseWriter.Header().Set("Access-Control-Max-Age", "1728000")
	ctx.ResponseWriter.Header().Set("Access-Control-Allow-Credentials", "true")
	ctx.ResponseWriter.Header().Set("Content-type", "application/json")

	// 预检请求
	if ctx.Input.Method() == http.MethodOptions {
		ctx.ResponseWriter.WriteHeader(http.StatusNoContent)
	}
}
